package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 套餐业务实现
 */
@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 条件查询
     *
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     *
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());

        Page<Setmeal> page = setmealMapper.pageQuery(setmealPageQueryDTO);
        int pageSize = page.getPageSize();
        List<Setmeal> result = page.getResult();
        return new PageResult(pageSize, result);
    }

    @Override
    @Transactional
    public void save(SetmealDTO setmealDTO) {
        //先对套餐添加
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmeal.setCreateTime(LocalDateTime.now());
        setmeal.setUpdateTime(LocalDateTime.now());
        setmeal.setCreateUser(BaseContext.getCurrentId());
        setmeal.setUpdateUser(BaseContext.getCurrentId());

        if (setmeal.getId() == null) {
            //插入，需要设置主键回显
            setmealMapper.insert(setmeal);
        } else {
            //拓展修改
        }
        Long id = setmeal.getId();
        //一个菜品只能给一个套餐关联
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        List<Long> dishIds = new ArrayList<>();
        for (SetmealDish setmealDish : setmealDishes) {
            dishIds.add(setmealDish.getDishId());
        }
        List<Long> ids = setmealDishMapper.getSetmealIdsByDishIds(dishIds);
        if (ids.size() > 0) {
            //表明被关联
            throw new SetmealEnableFailedException("已经被关联");

        }
        setmealDishes.forEach(setmealDish -> {

            setmealDish.setSetmealId(id);
        });
        setmealDishMapper.insertBatch(setmealDishes);
    }

    @Override
    public void setStatus(Long id, Integer status) {
        Setmeal build = Setmeal.builder()
                .status(status)
                .id(id).build();
        setmealMapper.updateSetmeal(build);
    }

    @Override
    @Transactional
    public SetmealVO getSetmealVoById(Long id) {
        Setmeal setmeal = setmealMapper.getById(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        List<SetmealDish> setmealDishById = setmealDishMapper.getSetmealDishById(setmeal.getId());
        setmealVO.setSetmealDishes(setmealDishById);
        return setmealVO;
    }

    @Override
    @Transactional
    public void deleteSetmeals(List<Long> ids) {
        //删除套餐 - 批量处理
        setmealMapper.delete(ids);
        //根据套餐id删除套餐菜品表
        setmealDishMapper.delete(ids);
    }

    @Override
    @Transactional
    public void updateSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO , setmeal);
        //修改套餐表
        setmealMapper.updateSetmeal(setmeal);

        //先删除对应的菜品
        List<Long> ids = new ArrayList<>();
        ids.add(setmealDTO.getId());
        setmealDishMapper.delete(ids);
        //添加对应菜品
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealDTO.getId());
        });
       setmealDishMapper.insertBatch(setmealDishes);


    }
}
